package com.example.fms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fms.dto.req.CategoriesReqDto;
import com.example.fms.dto.resp.CategoriesAttributesRespDto;
import com.example.fms.entity.Categories;
import com.example.fms.entity.CategoriesAttributes;
import com.example.fms.mapper.CategoriesAttributesMapper;
import com.example.fms.mapper.CategoriesMapper;
import com.example.fms.service.CategoriesAttributesService;
import com.example.fms.service.CategoriesService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class CategoriesAttributesImpl extends ServiceImpl<CategoriesAttributesMapper, CategoriesAttributes> implements CategoriesAttributesService {

    @Resource
    CategoriesAttributesMapper categoriesAttributesMapper;
    @Override
    public List<CategoriesAttributesRespDto> getAttributes(Long id, String sel) {
        List<CategoriesAttributesRespDto> categoriesAttributesRespDtoList = new ArrayList<>();
        LambdaQueryWrapper<CategoriesAttributes> qw = new LambdaQueryWrapper<>();
        qw.eq(CategoriesAttributes::getCatId,id).eq(CategoriesAttributes::getAttrSel,sel);
        List<CategoriesAttributes> categoriesAttributes = categoriesAttributesMapper.selectList(qw);
        if (!CollectionUtils.isEmpty(categoriesAttributes)) {
            categoriesAttributes.forEach(s -> {
                CategoriesAttributesRespDto categoriesAttributesRespDto = new CategoriesAttributesRespDto();
                categoriesAttributesRespDto.setAttr_id(s.getAttrId());
                categoriesAttributesRespDto.setCat_id(s.getCatId());
                categoriesAttributesRespDto.setAttr_name(s.getAttrName());
                categoriesAttributesRespDto.setAttr_sel(s.getAttrSel());
                categoriesAttributesRespDto.setAttr_vals(s.getAttrVals());
                categoriesAttributesRespDto.setAttr_write(s.getAttrWrite());
                categoriesAttributesRespDtoList.add(categoriesAttributesRespDto);
            });
        }
        return categoriesAttributesRespDtoList;
    }

    @Override
    public Boolean saveAttributes(Integer id, CategoriesReqDto req) {
        CategoriesAttributes categoriesAttributes = new CategoriesAttributes();
        categoriesAttributes.setCatId(id);
        categoriesAttributes.setAttrSel(req.getAttr_sel());
        categoriesAttributes.setAttrName(req.getAttr_name());
        categoriesAttributesMapper.insert(categoriesAttributes);
        return true;
    }

    @Override
    public CategoriesAttributesRespDto getAttributesById(Long id, Long attrId, String sel) {
        CategoriesAttributesRespDto categoriesAttributesRespDto = new CategoriesAttributesRespDto();
        LambdaQueryWrapper<CategoriesAttributes> qw = new LambdaQueryWrapper<>();
        qw.eq(CategoriesAttributes::getCatId,id)
                .eq(CategoriesAttributes::getAttrId,attrId);
        if (!StrUtil.isBlank(sel)) {
            qw.eq(CategoriesAttributes::getAttrSel,sel);
        }
        CategoriesAttributes categoriesAttributes = categoriesAttributesMapper.selectOne(qw);
        if (categoriesAttributes != null) {
            categoriesAttributesRespDto.setAttr_name(categoriesAttributes.getAttrName());
            categoriesAttributesRespDto.setAttr_sel(categoriesAttributes.getAttrSel());
            categoriesAttributesRespDto.setAttr_vals(categoriesAttributes.getAttrVals());
            categoriesAttributesRespDto.setAttr_write(categoriesAttributes.getAttrWrite());
            categoriesAttributesRespDto.setAttr_id(categoriesAttributes.getAttrId());
            categoriesAttributesRespDto.setCat_id(categoriesAttributes.getCatId());
        }
        return categoriesAttributesRespDto;
    }

    @Override
    public Boolean putAttributesById(Long id, Long attrId, CategoriesReqDto req) {
        LambdaQueryWrapper<CategoriesAttributes> qw = new LambdaQueryWrapper<>();
        qw.eq(CategoriesAttributes::getCatId,id)
                .eq(CategoriesAttributes::getAttrId,attrId)
                .eq(CategoriesAttributes::getAttrSel,req.getAttr_sel());
        CategoriesAttributes categoriesAttributes = categoriesAttributesMapper.selectOne(qw);
        if (Objects.nonNull(categoriesAttributes)){
            categoriesAttributes.setAttrName(req.getAttr_name());
            categoriesAttributes.setAttrVals(req.getAttr_vals());
            categoriesAttributesMapper.updateById(categoriesAttributes);
        }
        return true;
    }

    @Override
    public Boolean deleteAttributesById(Long id, Long attrId) {
        categoriesAttributesMapper.deleteById(attrId);
        return true;
    }
}
